package com.yly.doctorapp.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * ProjectName：WisdomBus
 * PackageName：com.zhcx.wisdombus.base
 * description:
 * Date：2016/9/5 9:35
 * Author：Steven
 * ClassName:BaseBusFragment
 **/
public abstract class BaseFragment extends Fragment {
    protected View mViewLayout;
    protected Context mContext;
    protected Unbinder unbinder;//绑定
    private int count;//记录开启进度条的情况 只能开一个
    //当前Fragment是否处于可见状态标志，防止因ViewPager的缓存机制而导致回调函数的触发
    private boolean isFragmentVisible;
    //是否是第一次开启网络加载
    public boolean isFirst;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        if (null == this.mViewLayout) {
            this.mViewLayout = inflater.inflate(this.getContentLayoutId(), container, false);
        }
        if (null != this.mViewLayout.getParent()) {
            ViewGroup parent = (ViewGroup) this.mViewLayout.getParent();
            parent.removeView(this.mViewLayout);
        }
        mContext = this.getActivity();
        unbinder = ButterKnife.bind(this, mViewLayout);
        this.initView(this.mViewLayout, savedInstanceState);
        this.initData();
        //可见，但是并没有加载过
        if (isFragmentVisible && !isFirst) {
            onFragmentVisibleChange(true);
        }
        return mViewLayout;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            isFragmentVisible = true;
        }
        if (mViewLayout == null) {
            return;
        }
        //可见，并且没有加载过
        if (!isFirst&&isFragmentVisible) {
            onFragmentVisibleChange(true);
            return;
        }
        //由可见——>不可见 已经加载过
        if (isFragmentVisible) {
            onFragmentVisibleChange(false);
            isFragmentVisible = false;
        }
    }

    /**
     * 视图id
     *
     * @return layout id
     */
    protected abstract int getContentLayoutId();

    /**
     * 初始化控件
     *
     * @param view
     * @param savedInstanceState
     */
    protected abstract void initView(View view, Bundle savedInstanceState);

    /**
     * Initialize the Activity data
     */
    protected abstract void initData();

    @Override
    public void onDestroy() {
        super.onDestroy();
        //解除绑定，官方文档只对fragment做了解绑
        unbinder.unbind();
    }
    /**
     * 当前fragment可见状态发生变化时会回调该方法
     * 如果当前fragment是第一次加载，等待onCreateView后才会回调该方法，其它情况回调时机跟 {@link #setUserVisibleHint(boolean)}一致
     * 在该回调方法中你可以做一些加载数据操作，甚至是控件的操作.
     *
     * @param isVisible true  不可见 -> 可见
     *                  false 可见  -> 不可见
     */
    protected void onFragmentVisibleChange(boolean isVisible) {

    }
}
